<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopProductGroup {

    var $db;
 	var $smarty;
 	
    function __construct($db, $smarty) {
    	$this->db = $db;
		$this->smarty = $smarty;
    }
    
    public function CreateProductGroup() {
		$arr = array();
		$arr['code'] = 0;
		$arr['message'] = "";
		$arr['sEdit'] 	= _("Edit");
		$arr['sDelete'] = _("Delete");
	
		if($arr['code'] == 0) {
			$arr['product_group_name'] = $_POST['product_group_name'];
			$arr['product_group_description'] = $_POST['product_group_description'];
			//$arr['product_list_status'] = $_POST['product_list_status'];
			
			$ret = "";				 
			$ret = BWebshopProductGroupBean::validate_product_list();
			if($ret == "") {
				$arr['ID'] = BWebshopProductGroupBean::save_product_list($this->db, $_POST);
				if( $arr['ID'] != -1) {
					$arr['code'] = 0;
					$arr['message'] = _("New Product Group has been saved.");
					header("Location: ./?module=BgcmsWebshop&sub1=productgroup");
					return;
				} else {
					$arr['code'] = 4;
					$arr['message'] = _("ERROR: Could not save Product List to database.");
				}
			} else {
				$arr = array('ID' => -1, 'message' => $ret);
			}
		}
		//echo array2json($arr);
	}
	
	public function EditProductGroup() {
		if(isset( $_POST['product_group_id']) ) {
			$msgId = 2;
			$msg = "";				 
			$msg = BWebshopProductGroupBean::validate_product_list($_POST);
			if($msg == "") {
				$res = BWebshopProductGroupBean::update_product_list($this->db, $_POST);	
				$message = "Product list has been updated";
				setcookie("message", $message,time()+5);
				header("Location: ./?module=BgcmsWebshop&sub1=productgroup&message=" . $msgId);
				return;
			} else {
				//we set msgId to 99 to indicate message will load in session variable
				$msgId = 99;
				$_SESSION['error_msg'] = $msg;
			}
			header("Location: ./?module=BgcmsWebshop&sub1=productlist&act=editProductGroup&message=" . $msgId . "&id=" . $_GET['id']);
		}
	}
	
	public function DeleteProductGroup() {
		$arr = array('code' => 0, 'message' => "");
		
		$id = @$_POST['id'];
		$res = BWebshopProductGroupBean::delete_product_list_by_id($this->db, $id);
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Product List <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Product List <u>%s</u>."), $id);
		}
		echo array2json($arr);
	}
	
}
?>